<?php 
// 20100418.01 Brian Smith
// Script to Graph the TR data
$conn = mysql_connect('MYSQLSERVER', 'USERNAME', 'PASSWORD') or die ('Error connecting to mysql');
mysql_select_db(pagetest); 
$domain=$_GET['domain'];
$startdatetime=$_GET['startdatetime'];
$enddatetime=$_GET['enddatetime'];
if ($startdatetime==null || $enddatetime==null) {
    $today=date("Y-m-d");
    $startdatetime="$today 00:00:00";
    $enddatetime="$today 23:59:59";
}
$dbcolumn=$_GET['dbcolumn'];
if ($dbcolumn==null) {
    $dbcolumn="TIME_TO_LOAD";
}
$int_y_pos = -1; 
$int_y_step_small = 1; 
$sql = "SELECT ID, DATETIME, $dbcolumn FROM IEWTR WHERE HOST = \"$domain\" AND DATETIME > \"$startdatetime\" AND DATETIME < \"$enddatetime\" ORDER BY DATETIME";
$sql = mysql_query($sql); 
$rownum = mysql_num_rows($sql); 
?>
<html> 
  <head> 
</script>
    <script type="text/javascript" src="http://www.google.com/jsapi"></script> 
    <script type="text/javascript"> 
function display_data(domain) {  
    xmlhttp=GetXmlHttpObject(); 
    if (xmlhttp==null) { 
        alert ("Your browser does not support AJAX!"); 
        return; 
    }  
    var url="graph_TR.php";
    url=url+"?domain="+domain; 
    xmlhttp.onreadystatechange=function() { 
        if (xmlhttp.readyState==4 || xmlhttp.readyState=="complete") {
            document.getElementById('Select_domain').innerHTML=xmlhttp.responseText;
        } 
    } 
    xmlhttp.open("GET",url,true); 
    xmlhttp.send(null); 
} 

function GetXmlHttpObject() { 
    var xmlhttp=null; 
    try { 
        // Firefox, Opera 8.0+, Safari 
        xmlhttp=new XMLHttpRequest(); 
    } 
    catch (e) { 
        // Internet Explorer 
        try { 
            xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); 
        } 
        catch (e) { 
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
        } 
    } 
    return xmlhttp; 
} 

      google.load("visualization", "1", {packages:["areachart"]});
      google.setOnLoadCallback(drawChart); 
      function drawChart() { 
        var data = new google.visualization.DataTable(); 
        data.addColumn('string', 'Date');
<?php 
echo "        data.addColumn('number', '$dbcolumn');\n"; 
echo "        data.addRows($rownum);\n";
$x=0;
while($row = mysql_fetch_assoc($sql)) { 
        $int_y_pos += $int_y_step_small; 
	$data1 = mysql_result($sql,$x,DATETIME);
	$data2 = mysql_result($sql,$x,$dbcolumn);
	$data3 = mysql_result($sql,$x,ID);
        echo "        data.setValue(" . $int_y_pos . ", 0, '" . $data1 . "');\n";
        echo "        data.setValue(" . $int_y_pos . ", 1, " . $data2 . ");\n"; 
        $x=$x+1;
}
        echo "var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));";
        echo "chart.draw(data, {width: 800, height: 640, legend: 'bottom', title: '$domain Site Performance'});";
echo "}</script></head><body>";
echo "<select onchange='window.location = \"graph_TR.php?domain=\"+(this.value)+\"&startdatetime=$startdatetime&enddatetime=$enddatetime&dbcolumn=$dbcolumn\"'>";
echo "<option>Select_domain</option>";
mysql_connect('MYSQLSERVER', 'USERNAME', 'PASSWORD') or die ('Error connecting to mysql');
mysql_select_db(pagetest);
$query = "SELECT HOST FROM IEWTR WHERE DATETIME > \"$startdatetime\" AND DATETIME < \"$enddatetime\" GROUP BY HOST";
$result=mysql_query($query);
while(list($domain, $name)=mysql_fetch_row($result)) { 
    echo "<option value=\"".$domain."\">".$domain."</option>"; 
}
?> 
</select>
    <div id="chart_div"></div> 
    <div id="Select_domain"></div>
  </body> 

</html> 
